<?php

use Lib\Dao\GestoresDao;
use Lib\Dao\PermisosDao;
use Lib\Dao\LogsDao;
use Lib\Dao\MenusDao;
use Lib\Utils\Funciones;
use Lib\Utils\MenusUtils;
use Lib\Utils\DatatablesUtils;
use Lib\Dao\UsuariosDao;
use Lib\Utils\UsuariosUtils;
use Lib\Utils\YuiCompressor;
use Illuminate\Support\Facades\Redirect;


class AdminController extends \BaseController {

	var $usuariosDao;
	var $menusDao;
	
	function __construct(){
		$this->usuariosDao = new UsuariosDao();
		$this->gestoresDao = new GestoresDao();
		$this->menusDao = new MenusDao();
	}
	
	public function index(){
		$this->getMenuUsuario();
		return Redirect::to($this->getVistaPortada());
	}
	
	
	public function cambioMenuUsuario(){
		if(Request::ajax()){
			$usuariosUtils = new UsuariosUtils();
			$result = $usuariosUtils->modificarMenuPredeterminado(Auth::user()->id,$_POST['id_gestor']);
			return Response::json($result);
		}
	}
		
	public function editorMenus(){
		$this->getMenuUsuario();
		$menusDao = new MenusDao();
		$this->layout->content = View::make('admin.editor_menus',array('info' =>json_encode($info))); 
	}

	public function crearUsuario(){
		$gestoresDao = new GestoresDao();
		$permisosDao = new PermisosDao();
		$this->getMenuUsuario();
		$info['gestores'] = $gestoresDao->getAllGestores();
		$info['permisos'] = $permisosDao->getAllPermisosUsuarios();

		$i=0;
		foreach($info['gestores'] as $gestor){
			
			foreach ($info['permisos'] as $permiso){
				$aux = new \stdClass();
				$aux->nombre = $gestor->nombre.' - '.$permiso->tipo;
				$aux->valor = $gestor->id.'.'.$permiso->id;
				$info['mes_gestors'][$i] = $aux;
				$i++;
			}
		}
		$this->layout->content = View::make('admin.crear_usuario', array('info' =>$info)); 
	}
		
	public function modificarUsuarios(){
		$this->comprobarPermisos(Auth::user()->rol,'1');
		$this->getMenuUsuario();		
		$this->layout->content = View::make('admin.modificar_usuarios');
	}
	
	public function mostrarLogs(){
		$this->getMenuUsuario();
		$this->layout->content = View::make('admin.mostrar_logs');
	}
	
	public function getAllGestores(){
		if(Request::ajax()){
			$result = $this->gestoresDao->getAllGestores();
			return Response::json(array('gestores' =>$result));
		}
	}
	
	public function getAllLogs(){
		if(Request::ajax()){
			return Response::json(DatatablesUtils::montarArrayLogsAdmin(LogsDao::getAllLogs()));
		}
	}
	
	public function vistaError(){
		$this->getMenuUsuario();
		$this->layout->content = View::make('admin.error',array('mensaje' =>"No tienes permisos"));
	}
	
	
	public function gestionMenusGestores(){
		$this->getMenuUsuario();
		$menus = new MenusDao();
		$menus = $menus->getAllMenus();
		$this->layout->content = View::make('admin.editor_menus');
	}
	
	public function compilar(){
		$this->getMenuUsuario();
		$result = Funciones::compilar();
		$this->layout->content = View::make('admin.compilar',array('result'=>$result));
	}
	
	public function getAllMenusPorIdGestor(){
		if(Request::ajax()){
			$menusDao = new MenusDao();
			$menus = MenusUtils::ordenarMenusConSubmenus($menusDao->getMenusPerIdGestor($_POST['id']));
			$result['menus'] = $menus;
			$result['listado_menus'] = MenusUtils::montarListadoNestableMenus($menus);
			return Response::json($result);
		}
	}
	
	//Vista con el formulario para reportar un error
	public function bug(){
		$this->getMenuUsuario();
		$this->layout->content = View::make('admin.reportar_error'); //hace render de la vista portada/inicio en la plantilla por defecto,y envia a la vista la variable
	}
	
	public function infoPlantilla(){
		$this->getMenuUsuario();
		$this->layout->content = View::make('admin.info_plantilla'); //hace render de la vista portada/inicio en la plantilla por defecto,y envia a la vista la variable
	}
	
	function insertarError(){
		if(Request::ajax()){
			$result = Funciones::getPost();
			return Response::json(LogsDao::nuevoBug($result->texto));
		}
	}
	
	function ultimaConexion(){
		$this->getMenuUsuario();
		$this->layout->content = View::make('admin.ultima_conexion');
	}
	
	function notificacionsError(){
		$this->getMenuUsuario();
		$this->layout->content = View::make('admin.notificacions_error');
	}
	
	function listadoErrores(){
		if(Request::ajax()){
			return Response::json(DatatablesUtils::listadoNotificacionErrores(LogsDao::getBugs()));
		}
	}
	
	function infoServer(){
		$this->layout = 'blank';
		phpinfo();
	}
	
	public function __destruct(){
		$this->usuariosDao;
		$this->gestoresDao;
		$this->menusDao;
	}

}

?>